//
//  AddGameDetailHomeViewController.swift
//  FootballDataRecord
//
//  Created by Xx Jiang on 2020/1/7.
//  Copyright © 2020 Xx Jiang. All rights reserved.
//

import UIKit
import SnapKit
class AddGameDetailHomeViewController: BaseViewController,UIScrollViewDelegate{
    @IBOutlet weak var scrollView: UIScrollView!
    var pageControl:UIPageControl!
    var currenPage = 1

    var teamAModel:TeamModel?
    var teamBModel:TeamModel?
    var page:Int = 1
    var matchModel:MatchModel? {
        didSet{
            teamAModel = TeamModel.getTeamModelWithTeamID(teamId: self.matchModel!.matchTeamA!)
            teamBModel = TeamModel.getTeamModelWithTeamID(teamId: self.matchModel!.matchTeamB!)

        }
    }

    override func viewWillAppear(_ animated: Bool) {
        let gameOverBt = UIButton.init(type: .custom)
        gameOverBt.frame = CGRect.init(x: 0, y: 0, width: 30, height: 44)
        gameOverBt.setTitle("结束比赛", for: .normal)
        gameOverBt.addTarget(self, action: #selector(clickGameOverBt), for: .touchUpInside)
        super.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: gameOverBt)
    }
    @objc func clickGameOverBt()  {
       //比赛结束
        matchModel?.matchStatus = "1"
        DHDataBaseOperate.changeValuesWithModel(model: matchModel!, keys: ["mainKey":DHMatchId,"valueKey":DHMatchStatus]) { (rs) in
            if rs.isEqual(1){
               print("比赛结束")
            }
        }
        self.navigationController?.popToRootViewController(animated: true)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupScrollView()
        
    }
    func setupScrollView() {
        self.scrollView.delegate = self
        self.scrollView.bounces = false
        self.scrollView.backgroundColor = .black
        self.scrollView.contentSize = CGSize.init(width: 3 * KScreenWidth, height: 0)
        self.view.addSubview(self.scrollView)
        
        let teamAVC = TeamDetailViewController()
        teamAVC.view.frame = CGRect.init(x: 0, y: 0, width:KScreenWidth, height: self.scrollView.frame.size.height)
        self.addChild(teamAVC)
        self.scrollView.addSubview(teamAVC.view)

        let gameDetail = AddGameDetailViewController()
        gameDetail.view.frame = CGRect.init(x: KScreenWidth, y: 0, width:KScreenWidth, height: self.scrollView.frame.size.height)

        gameDetail.teamA = teamAModel
        gameDetail.teamB = teamBModel
        gameDetail.matchModel = matchModel

        self.addChild(gameDetail)
        self.scrollView.addSubview(gameDetail.view)
        
        let teamBVC = TeamDetailViewController()
        self.addChild(teamBVC)
        teamBVC.view.frame = CGRect.init(x: KScreenWidth*2, y: 0, width:KScreenWidth, height: self.scrollView.frame.size.height)
        self.scrollView.addSubview(teamBVC.view)
        
        self.scrollView.isPagingEnabled = true
        self.pageControl = UIPageControl.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        self.view.addSubview(pageControl)
        pageControl.numberOfPages = 3
        pageControl.currentPage = self.currenPage
        self.scrollView.contentOffset = CGPoint.init(x: KScreenWidth, y: 0)
        pageControl.currentPageIndicatorTintColor = UIColor.black
        pageControl.pageIndicatorTintColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
        pageControl.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-KBottomSafe)
            make.width.equalTo(100)
            make.height.equalTo(30)
            make.centerX.equalToSuperview()
        }
        pageControl.addTarget(self, action: #selector(clickPageControl), for: .touchUpInside)
    }
    @objc func clickPageControl(control:UIPageControl){
        print("currentpage\(pageControl.currentPage)")
        if currenPage != control.currentPage {
             currenPage = pageControl.currentPage
            scrollView.contentOffset = CGPoint.init(x: Int(KScreenWidth) * control.currentPage , y: 0)
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print(scrollView.contentOffset.x)
        if scrollView.contentOffset.x == 0 {
            page = 0
        }else if(scrollView.contentOffset.x == (scrollView.contentSize.width - KScreenWidth)){
            page = 2
        }else{
            page = 1
        }
        
        if  self.pageControl.currentPage != page{
            self.pageControl.currentPage = page
        }
        if page == 0{
            //发送通知告诉左边的控制器刷新数据
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeTeam`sPlayerData"), object: nil, userInfo: ["team":AddGameDetailViewController.TeamType.leftTeam,"matchModel":matchModel as Any])
        }
        if page == 2 {
            //发送通知告诉左边的控制器刷新数据
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "changeTeam`sPlayerData"), object: nil, userInfo: ["team":AddGameDetailViewController.TeamType.rightTeam,"matchModel":matchModel as Any])

        }
        
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
        print("取消监听")
    }

}
